package com.talkweb.securitycamera.common.base;

import com.talkweb.securitycamera.common.base.utils.ResourceHelper;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.global.resource.Resource;

/**
 * @version 1.0.1
 * @description:
 * @program: security-camera
 * @Author xiaozhijun
 * @Date 2021/1/25 17:10
 */
public abstract class BaseFraction extends Fraction {
    private Component mComponent;
    /**
     * 获取布局Id
     *
     * @return 布局文件id
     */
    public abstract int getLayoutId();

    /**
     * 初始化组件
     *
     * @param component root组件
     */
    public abstract void initializeComponent(Component component);

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        mComponent = scatter.parse(getLayoutId(), container, false);
        initializeComponent(mComponent);
        return mComponent;
    }

    @Override
    public void terminateAbility() {
        super.terminateAbility();
        getFractionAbility().terminateAbility();
    }

    /**
     * 设置Ability返回result
     *
     * @param resultCode
     * @param resultData
     */
    public final void setResult(int resultCode, Intent resultData) {
        getFractionAbility().setResult(resultCode, resultData);
    }

    /**
     * 发现组件
     *
     * @param resId 资源id
     * @param <T>   组件类型
     * @return 组件对象
     */
    protected final <T extends Component> T findComponent(int resId) {
        return (T) mComponent.findComponentById(resId);
    }

    /**
     * 启动Ability
     *
     * @param intent intent对象
     */
    protected final void startAbility(Intent intent) {
        getFractionAbility().startAbility(intent);
    }

    /**
     * 获取资源文件中的String
     *
     * @param resourceId 资源id
     * @return 资源文件中对应字符串
     */
    public final String getString(int resourceId) {
        return ResourceHelper.getString(getFractionAbility(), resourceId);
    }

    /**
     * 获取资源文件中的Resource
     *
     * @param resourceId 资源id
     * @return Resource 资源对象
     */
    public final Resource getResource(int resourceId) {
        return ResourceHelper.getResource(getFractionAbility(), resourceId);
    }

}
